﻿using BlamanticUI.Abstractions;

namespace BlamanticUI.Components;

/// <summary>
/// Represents a child component of <see cref="Accordion"/> component.
/// </summary>
public class AccordionItem : BlazorChildComponentBase<Accordion, AccordionItem>, IHasActive,IHasChildContent
{
    /// <summary>
    /// Initializes a new instance of <see cref="AccordionItem"/> class.
    /// </summary>
    public AccordionItem()
    {
        Title = "AccrodionItem" + Index + 1;
    }

    /// <summary>
    /// Gets or sets a UI content of title.
    /// </summary>
    [Parameter]
#if NET6_0_OR_GREATER
    [EditorRequired]
#endif
    public string Title { get; set; }
    /// <summary>
    /// Determined a state of component can be actived.
    /// </summary>
    [Parameter] public bool Active { get; set; }
    /// <summary>
    /// A segment of UI content to render.
    /// </summary>
    [Parameter]public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Gets the get active class.
    /// </summary>
    /// <value>
    /// The get active class.
    /// </value>
    string GetActiveClass => Active ? "active " : null;

    /// <summary>
    /// Builds the render tree.
    /// </summary>
    /// <param name="builder">The builder.</param>
    /// <returns></returns>
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.CreateElement(0, TagName, content =>
        {
            content.CreateComponent<Icon>(0, attributes: new { IconClass = "dropdown" });
            content.AddContent(1, Title);
        }, new
        {
            @class = $"{GetActiveClass}title",
            onclick = EventCallback.Factory.Create<MouseEventArgs>(this, async () => await ParentComponent.Activate(Index))
        });


        builder.CreateComponent<Content>(1, ChildContent, new { AdditionalCssClass = GetActiveClass });
    }
}
